/**
 * 309. 最佳买卖股票时机含冷冻期
 * https://leetcode-cn.com/problems/best-time-to-buy-and-sell-stock-with-cooldown/
 */

/**
 * @param {number[]} prices
 * @return {number}
 */
function maxProfit(prices) {
  // 持有
  // 不持有但处于冷冻期
  // 不持有且不处于冷冻期
  const dp = new Array(prices.length);
  for (let i = 0; i < prices.length; i += 1) {
    dp[i] = [0, 0, 0];
  }
  dp[0][0] = -prices[0];
  for (let i = 1; i < prices.length; i += 1) {
    const cur = dp[i];
    const pre = dp[i - 1];
    cur[0] = Math.max(pre[2] - prices[i], pre[0]);
    cur[1] = pre[0] + prices[i];
    cur[2] = Math.max(pre[1], pre[2]);
  }
  return Math.max(dp[prices.length - 1][1], dp[prices.length - 1][2]);
}

console.log(maxProfit([1, 2, 3, 0, 2]) === 3);
console.log(maxProfit([1]) === 0);
console.log(maxProfit([1, 2, 4]) === 3);
